package com.lethelh.jdbc.utils;

import java.sql.*;

/**
 * 封装jdbc工具类
 * @author jasonper
 */
public class DBUtils {
    /**
     * 获取jdbc数据库连接
     * @return Connection
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimeZone=Asia/Shanghai";
            //        jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimeZone=Asia/Shanghai
        return DriverManager.getConnection(url,"root","123456");
    }

    /**
     * 关闭数据库连接
     * @param rs
     * @param stmt
     * @param conn
     * @throws SQLException
     */
    public static void closeConnection(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
        if (rs != null){
            rs.close();
        }
        if (stmt != null){
            stmt.close();
        }
        if (conn != null && !conn.isClosed()){
            conn.close();
        }
    }
}
